package dashboard;

import java.util.*;
import javax.persistence.Embedded;
import java.io.Serializable;

import javax.persistence.Id;

@SuppressWarnings("serial")
public class User implements Serializable{

	public enum Role {
		TUTOR, STUDENT;
	}

	@Id Long id;
	
	private String name;
	private String firstName;
	private String userName;
	private String password;
	private String email;
	private String number;
	private boolean timerStatus;
	private List<Long> courses;
	private Date lastLogin;
	private Date previousLastLogin;
	@Embedded private List<Activity> activities;
	private List<String> favourites;
	@Embedded private List<Milestone> milestones;
	private int treshold;

	private Role role = Role.STUDENT;

	public User(String username, String password, String email, List<Long> courses, String role, String firstName, String name, String number)
	{
		this.userName = username;
		this.password = password;
		this.email = email;
		this.courses = courses;
		this.name = name;
		this.firstName = firstName;
		this.setNumber(number);
		if(role.equals("Tutor"))
			this.role = Role.TUTOR;

		this.activities = new ArrayList<Activity>();
		this.milestones = new ArrayList<Milestone>();
		this.favourites =  new ArrayList<String>();
		this.timerStatus = false;
		this.treshold = 20;
	}

	public User()
	{
		this.activities = new ArrayList<Activity>();
		this.milestones = new ArrayList<Milestone>();
		this.favourites =  new ArrayList<String>();
		courses = new ArrayList<Long>();
	}

	// Getters and setters

	public boolean setPassword(String password)
	{
		this.password = password;
		return true;
	}

	public boolean getTimerStatus() {
		return timerStatus;
	}

	public void setTreshold(int tresh){
		this.treshold = tresh;
	}
	
	public int getTreshold(){
		return treshold;
	}
	public List<String> getFavourites() {
		return favourites;
	}


	public List<Milestone> getMilestones() {
		return milestones;
	}


	public boolean setEmail(String email)
	{
		this.email = email;
		return true;
	}

	public String getUserName()
	{
		return userName;
	}

	public String getPassword()
	{
		return password;
	}

	public Role getRole()
	{
		return role;
	}

	public String getEmail()
	{
		return email;
	}

	public List<Long> getCourses()
	{
		return courses;
	}

	public List<Activity> getActivities()
	{
		return activities;
	}

	public long getId()
	{
		return id;
	}

	//adders and remover

	public boolean addCourse(Long courseId)
	{
		courses.add(courseId);
		return true;
	}

	public boolean removeCourse(Long courseId)
	{
		if(courses.size() == 0) return false;
		for(Long course: courses)
		{
			if(course==courseId)
			{
				courses.remove(course);

				return true;
			}
		}
		return false;
	}


	public void addActivity(Activity activity)
	{		activities.add(activity);
	}

	/**
	 * @param timing true if user is currently timing. false if not timing.
	 */
	public void setTiming(boolean timing){
		this.timerStatus= timing;
	}
/*	public boolean removeActivity(long activityId)
	{
		if(activities.size() == 0) return false;
		for(Activity activity: activities)
		{
			if(activity.getId() == activityId)
			{
				activities.remove(activity);
				return true;
			}

		}
		return false;
	}*/

	public Milestone getMilestoneById(long Id){ 
		
			for(Milestone milestone: milestones){
				if(milestone.getId()== Id)
				return milestone;
			
		}
		return null;
	}
	
	
	public void addFavourite(String userName) {
		favourites.add(userName);
	}

	/**
	 * Verwijdert bepaalde user uit favorieten
	 * @param user : te verwijderen gebruiker uit favorieten
	 * @return true als gelukt, false als mislukt
	 */
	public boolean removeFavourite(String userName)
	{
		if(favourites.size() == 0) return false;
		if(favourites.contains(userName))
		{
			favourites.remove(userName);
			return true;
		}
		return false;
	}


	public void addMilestone(Milestone milestone)
	{
		milestones.add(milestone);
	}

	public boolean removeMilestone(long milestoneId)
	{
		if(milestones.size() == 0) return false;
		for(Milestone milestone: milestones)
		{
			if(milestone.getId() == milestoneId)
			{
				milestones.remove(milestone);
				return true;
			}

		}
		return false;
	}

	public void setUsername(String username) 
	{
		this.userName = username;
	}

	public void setActivity(Activity activity){
		this.activities.set(activities.size()-1, activity);
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}
	public void setRole(String role)
	{
		if(role.equals("tutor"))
			this.role = Role.TUTOR;
		else
			this.role = Role.STUDENT;
	}
	public void clearCourses()
	{
		courses = new ArrayList<Long>();
	}

    public Date getLastLogin()
    {
        return lastLogin;
    }
    public void setLastLogin(Date date)
    {
        lastLogin = date;
    }
    public void setPreviousLastLogin(Date date)
    {
        previousLastLogin = date;
    }
    public Date getPreviousLastLogin()
    {
        return previousLastLogin;
    }

	
}
